<?php

namespace Question\Service;

/**
 * 抢答业务
 */
class Rush {
	private $_daoQuestionModel;
	private $_daoDeviceModel;

	/**
	 * 构造函数
	 */
	public function __construct() {
		$this->_daoQuestionModel = \Lsf\Loader::Model('DaoQuestion', TRUE);
		$this->_daoDeviceModel = \Lsf\Loader::Model('DaoDevice', TRUE);
	}

	/**
	 * 抢答
	 */
	public function executeRush($deviceId, $groupId, $questionId){
		// 查主持人设备
		$where = [
			'group_id' => 3,
			'status' => 1,
		];
		$device = $this->_daoDeviceModel->select('id,device_id,group_id,center_question_id,re_rush_question_id,rush_group_id,rush_start_flag', $where, 'update_time DESC', 1, FALSE);
		if($device[0]['re_rush_question_id'] == $questionId && $device[0]['rush_group_id'] == $groupId){
			return [
				'is_success' => TRUE,
			];
		}
		// 抢答标记
		if($device[0]['rush_start_flag'] != 1){
			return -1;
		}
		// 抢答
		$upDevice = [
			're_rush_question_id' => $questionId,
			'rush_group_id' => $groupId
		];
		$where = [
			'id' => $device[0]['id'],
			're_rush_question_id' => ['neq' , $questionId]
		];
		$rows = $this->_daoDeviceModel->update($upDevice, $where);
		if($rows == 1 ){
			$isSuccess = TRUE;
		}else{
			$isSuccess = FALSE;
		}
		return [
			'is_success' => $isSuccess,
		];
	}

	/**
	 * 抢答结果
	 */
	public function getRushResult($deviceId){
		// 查主持人设备
		$where = [
			'device_id' => $deviceId,
		];
		$device = $this->_daoDeviceModel->select('id,center_question_id,rush_group_id,re_rush_question_id,rush_user_answer', $where, 'id DESC', 1, FALSE);
		if($device[0]['center_question_id'] == 0 || $device[0]['center_question_id'] == $device[0]['re_rush_question_id']) {
			return [
				'user_rush_answer' => $device[0]['rush_user_answer'],
				'group_id'   => $device[0]['rush_group_id'],
				'group_name' => $this->_getGroupName($device[0]['rush_group_id']),
			];
		}else{
			return [
				'user_rush_answer' => '',
				'group_id'   => 0,
				'group_name' => '',
			];
		}
	}

	/**
	 * 抢答分数
	 */
	public function getRushPoint(){
		// 查主持人设备
		// 查主持人设备
		$where = [
			'group_id' => 3,
			'status' => 1,
		];
		$device = $this->_daoDeviceModel->select('id,device_id,rush_gp_a,rush_gp_b', $where, 'update_time DESC', 1, FALSE);
		return [
			'group_a' => $device[0]['rush_gp_a'],
			'group_b' => $device[0]['rush_gp_b'],
		];
	}

	/**
	 * 抢答后答题
	 */
	public function rushAnswer($deviceId, $groupId, $questionId, $answer){
		// 查主持人设备
		$where = [
			'group_id' => 3,
			'status' => 1,
		];
		$device = $this->_daoDeviceModel->select('id,rush_group_id,rush_gp_a,rush_gp_b,rush_question_id', $where, 'update_time DESC', 1, FALSE);
		// 判断抢答组别
		if($device[0]['rush_group_id'] != $groupId){
			return [$device];
		}
		// 判断是否已经答题
		if($device[0]['rush_question_id'] == $questionId){
			return [3];
		}
		// 获取题目
		$isAnswerTrue = FALSE;
		$question = $this->_daoQuestionModel->select('question_id, answer', ['question_id' => $questionId], 'question_id DESC', 1, FALSE);
		// 判断答案正误
		if($question[0]['answer'] == $answer){
			$isAnswerTrue = TRUE;
		}
		// 准备更新设备信息
		$upDevice = ['rush_question_id' => $questionId];
		// 答对了则加分
		if($isAnswerTrue) {
			if ($groupId == 1) {
				$point = $device[0]['rush_gp_a'] + 10;
				$upDevice['rush_gp_a'] = $point;
			}
			if ($groupId == 2) {
				$point = $device[0]['rush_gp_b'] + 10;
				$upDevice['rush_gp_b'] = $point;
			}
		}
		$where = [
			'id'               => $device[0]['id'],
			'rush_question_id' => ['neq', $questionId]
		];
		// 记录用户答案
		$upDevice['rush_user_answer'] = $answer;
		$rows = $this->_daoDeviceModel->update($upDevice, $where);
		if($rows != 1){
			\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
				'model_name' => 'dao_device',
				'update'     => $upDevice,
				'where'      => $where
			]);
		}
		return [4];
	}

	/**
	 * 抢答重置
	 */
	public function rushRestart($deviceId){
		// 查主持人设备
		$where = [
			'device_id' => $deviceId,
		];
		$device = $this->_daoDeviceModel->select('id,device_id,group_id,set_id', $where, 'id DESC', 1, FALSE);
		if($device[0]['group_id'] != 3){
			return -1;
		}
		$where = [
			'device_id' => $deviceId,
		];
		if($device[0]['set_id'] == 1){
			$setId = 2;
		}else{
			$setId = 1;
		}
		$up = [
			'is_show' => 0,
			'center_question_id' => 0,
			'rush_group_id' => 0,
			'rush_gp_a' => 0,
			'rush_gp_b' => 0,
			'rush_question_id' => 0,
			're_rush_question_id' => 0,
			'rush_user_answer' => '',
			'rush_start_flag' => 2,
			'set_id' => $setId
		];
		$rows = $this->_daoDeviceModel->update($up, $where);
		return [
			'ok' => 'ok'
		];
	}

	/**
	 * 开始抢答
	 */
	public function rushStart($deviceId){
		$where = [
			'device_id' => $deviceId,
		];
		$up = [
			'rush_start_flag' => 1
		];
		$rows = $this->_daoDeviceModel->update($up, $where);
		return ['ok'=>'ok'];
	}

	private function _getGroupName($groupId){
		switch ($groupId){
		case 3 :
			{
				$groupName = '主持人';
				break;
			}
		case 1 :
			{
				$groupName = 'A组';
				break;
			}
		case 2 :
			{
				$groupName = 'B组';
				break;
			}
		default :
			{
				$groupName = '';
			}
		}
		return $groupName;
	}
}